/*
The surface-physics project: http://code.google.com/p/surface-physics/
Adds real world physics based on the Bullet physics engine, to bases in the Orbiter space
flight simulation software.

Written by Abhijit Nandy
License : Apache License 2.0


BaseSelector implementation


*/

#include "BaseSelector.h"

#include "../BulletBase/Bump_BulletBase.h"
#include "../Engine/PhysicsEngine.h"

// Declared in Bump.cpp
extern BaseSelector *gBaseSelector;
extern Bump_BulletBase *base[];
extern int gNumBases;


// ==============================================================
// nonmember functions

void OpenDialog (void *context)
{
	((BaseSelector*)context)->OpenDialog();
}

BOOL CALLBACK BaseSelectorProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	return gBaseSelector->MsgProc (hDlg, uMsg, wParam, lParam);
}


// ==============================================================
// BaseSelector class definition
// ==============================================================

BaseSelector::BaseSelector (HINSTANCE hDLL)
{
	hInst  = hDLL;
	hDlg   = NULL;

	dwCmd = oapiRegisterCustomCmd (
		"Visualizer Simbox Selector",
		"View a different simbox than the one focused on currently in Orbiter",
		::OpenDialog, this);
}

BaseSelector::~BaseSelector ()
{
	CloseDialog();
	oapiUnregisterCustomCmd (dwCmd);
}

void BaseSelector::OpenDialog ()
{
	hDlg  =  oapiOpenDialog (hInst, IDD_MYDIALOG, BaseSelectorProc);

	// Add list of bases to base selection list box
	SendDlgItemMessage (hDlg, IDC_BASELIST, LB_RESETCONTENT, 0, 0);

	SendDlgItemMessage (hDlg, IDC_BASELIST, LB_INSERTSTRING, 0, (LPARAM)"Brighton Beach");
	//SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM) i);

	SendDlgItemMessage (hDlg, IDC_BASELIST, LB_INSERTSTRING, 0, (LPARAM)"Olympus");
	//SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM) i);
}

void BaseSelector::CloseDialog ()
{
	if (hDlg) {
		oapiCloseDialog (hDlg);
	}
}


void BaseSelector::InitDialog (HWND _hDlg)
{
	hDlg = _hDlg;

	// No point adding stuff to list boxes here, the dialog control/item may not have been
	// initialized and will not receive the messages

}



int BaseSelector::MsgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	char name[256];

	switch (uMsg) {
	case WM_INITDIALOG:
		sprintf (name, "%d", 2);
		SetWindowText (GetDlgItem (hDlg, IDC_REMEMBER), name);
		return TRUE;

	case WM_DESTROY:
		GetWindowText (GetDlgItem (hDlg, IDC_REMEMBER), name, 256);
		sprintf (name, "%d", 2);
		return TRUE;

	// Each dialog item has a separate case here
	case WM_COMMAND:
		switch (LOWORD (wParam)) {

		case IDC_WHOAMI:  // user pressed dialog button
			// display the focus vessel name
			oapiGetObjectName (oapiGetFocusObject(), name, 256);
			SetWindowText (GetDlgItem (hDlg, IDC_IAM), name);
			return TRUE;

		case IDCANCEL: // dialog closed by user
			CloseDialog ();
			return TRUE;

		case IDC_BASELIST:
			{
				switch (HIWORD(wParam))
				{
				case LBN_SELCHANGE:
					{
						HWND hwndList = GetDlgItem(hDlg, IDC_BASELIST);

						// Get selected index.
						int lbItem = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0);

						if (lbItem == 0)
							base[1]->setCameraFocus(VCM_GUI);  //hack, 0 & 1 opp. as list box rearranges
						else
							base[0]->setCameraFocus(VCM_GUI);

						// Get item data.
						//int i = (int)SendMessage(hwndList, LB_GETITEMDATA, lbItem, 0);

						// Do something with the data
						//MessageBox(NULL, "MessageBox Text", "MessageBox caption", MB_OK);
						return TRUE;
					}
				}
			}
		return TRUE;

		} // end switch (LOWORD (wParam))
		break;
	}

	return oapiDefDialogProc (hDlg, uMsg, wParam, lParam);
}


void BaseSelector::Pause (bool pause)
{
	//if (hDlg) oapiSetTitleButtonState (hDlg, IDPAUSE, pause ? 1:0);
}




